package org.java.web;

import org.java.dao.ArtUserDao;
import org.java.dao.DaoImpl.UserDaoImpl;
import org.java.util.MailUtils;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet("/user")
public class UserServlet extends BaseServlet{
    private ArtUserDao dao=new UserDaoImpl();


    //登录的方法
    protected void sign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");


        //进入dao层判断
        boolean flag=dao.sign(name,pwd);
        //由此用户取出uuid（主键）
        String UserId;
        if(flag){
           UserId=dao.getUserId(name,pwd);
           request.getSession().setAttribute("userId",UserId);
        }

        //创建输出流返回结果
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(flag){
            //true 有此用户
            request.getSession().setAttribute("userName",name);
            out.write("yes");
        }else {
            out.write("no");
        }
        //关闭流
        out.flush();
        out.close();
    }

    //注册的方法
    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String emialyzm = request.getParameter("emialyzm");
        String phone = request.getParameter("phone");
        String pwd = request.getParameter("pwd");
        String pwdAgin = request.getParameter("pwdAgin");

        String rollBack="";

        boolean phoneFlag=phone.matches("^1[3,5,7,8,9]\\d{9}$");

        //准备好输出流准备回调输出
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();

        //取出验证码
        HttpSession session=request.getSession();
        String code = (String) session.getAttribute("code");

        System.out.println(code);

        /**判断用户是否点了获取验证码**/
//        if(code==null) {
//            rollBack = "请获取验证码";
//        }

//        /**判断密码两次填写是否正确**/
//        if(!pwd.equals(pwdAgin)){
//            rollBack="密码填写有误";
//        }
//        if(email==null||email.equals("")){
//            rollBack="邮箱为空";
//        }
//        if(!emialyzm.equals(code)){
//            rollBack="验证码错误";
//        }
        /**判断用户名是否填写**/
//        if(name==null||name.equals("")){
//            rollBack="姓名为空";
//        }
        boolean flag = dao.judegEmial(email);
        System.out.println(flag);

        //因为不是submit提交判断，非空属性不能用，所以要进servlet判断
        if(name!=null&&!name.equals("")){
            if(email!=null&&!email.equals("")){
                if(emialyzm!=null&&!emialyzm.equals("")){
                    if(pwd!=null&&!pwd.equals("")){
                        if(pwdAgin!=null&&!pwdAgin.equals("")){
                            if(emialyzm.equals(code)){
                                if(pwd.equals(pwdAgin)){
                                   if(!flag){
                                      if(phoneFlag){
                                          /**所有判断正确，入库**/
                                          System.out.println("最后异步");
                                          rollBack="完成";
                                          dao.userAdd(name,pwd,email,phone);
                                      }else {
                                          rollBack="手机格式错误";
                                      }
                                   }else {
                                       rollBack="邮箱已被注册";
                                   }
                                }else {
                                    rollBack="密码填写有误";
                                }
                            }else {
                                rollBack="验证码错误";
                            }
                        }else {
                            rollBack="二次密码为空";
                        }
                    }else {
                        rollBack="密码为空";
                    }
                }else {
                    rollBack="验证码为空";
                }
            }else {
                rollBack="邮箱为空";
            }
        }else{
            rollBack="姓名为空";
        }
        System.out.println(rollBack );


        out.write(rollBack);
        out.flush();
        out.close();

    }

    //获取验证码-->方法
    protected void yzyx(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, MessagingException {
        //获取邮箱
        String yzm = request.getParameter("yzm");
        //生成随机的验证码
        int code =(int)((Math.random()*9+1)*100000);
        String codee = String.valueOf(code);
        //存之session等下判断是否填写正确
        request.getSession().setAttribute("code",codee);

        //用右键模板 发送邮件
        MailUtils.sendMail(yzm,codee);

        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.write("yes");
        out.flush();
        out.close();

    }

    //name框失去焦点后的事件
    protected void judegName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        boolean flag = dao.sense(name);

        //创建流
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        String fg="";
        if(flag){
            //已有用户，不让注册
            fg="不行";
        }else {
            //可以注册
            fg="可以";
        }
        out.write(fg);
        out.flush();
        out.close();
    }

    //判断邮箱是否为空，包括格式
    protected void judegMail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建输出流
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();


        String mail = request.getParameter("mail");
        System.out.println(mail);




        String rollback="";
        if(mail==null||mail.equals("")){
            rollback="不能为空";
        }else {
            String check = "\\w+\\x40\\w+\\x2e\\w+";

            Pattern regex = Pattern.compile(check);

            Matcher matcher = regex.matcher(mail);

            boolean isMatched = matcher.matches();

            if(!isMatched){
                rollback="错误格式";
            }else {
                //去判断表中邮箱是否被占用
                boolean flag=dao.judegEmial(mail);
                if(flag){
                    out.write("邮箱已被注册");
                }
            }

        }

        out.write(rollback);
        out.flush();
        out.close();
    }

    //60秒后验证码失效方法
    protected void removeyzm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        session.removeAttribute("code");
    }

    //忘记密码-->提交表单
    protected void forget(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String yzm = request.getParameter("yzm");
        String pwd = request.getParameter("pwd");

        //获取session存的邮箱验证码
        String code = (String) request.getSession().getAttribute("code");

        //创建输出流，方便等下回调输出
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();

        //判断数据库里是否有此邮箱用户
        boolean exits=dao.judegEmial(email);

        if(exits){
            //判断数据表里是否有用户名 和 邮箱一致的用户
            boolean flag=dao.forget(name,email);

            if(yzm.equals(code)){
                if(flag){
                    //验证码 邮箱 用户民都符合 是本用户 可以改密码了
                    dao.reSetPwd(name,pwd);
                    System.out.println("已修改");
                    request.setAttribute("name",name);
                    out.write("完成");
                }else {
                    out.write("用户名填写错误");
                }
            }else {
                out.write("验证码输入错误");
            }
        }else {
            out.write("没有此邮箱用户");
        }


        //关闭输出流
        out.flush();
        out.close();
    }

    //手机失去焦点事件
    protected void judegPhone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone = request.getParameter("phone");
        boolean flag = phone.matches("^1[3,5,7,8,9]\\d{9}$");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(flag){
            out.write("yes");
        }else {
            out.write("no");
        }

        out.flush();
        out.close();
    }
}
